home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / FILE.C < prev    next >
Text File  |  1989-12-30  |  10KB  |  268 lines

  1. /*     FILE.C   FILE.C    FILE.C    FILE.C    FILE.C    FILE.C
  2.  
  3. FILE INPUT AND OUTPUT         XXXXX  XXX  X      XXXXX
  4.                               X       X   X      X
  5. May 27, 1986                  X       X   X      X
  6.                               XXX     X   X      XXX
  7.                               X       X   X      X
  8.                               X       X   X      X
  9.                               X      XXX  XXXXX  XXXXX
  10. */
  11. #include "stdio.h"
  12. #include "struct.def"
  13. int lastline = 0;
  14. int arrowln = 0;
  15. extern struct lines *top, *bot, *q, *p, *arrow, *trnsend;
  16. extern struct vars allvars[];
  17. extern int varinuse;
  18. extern int printit;
  19. extern char inline[];
  20. extern int errcode;
  21. extern int ignore;
  22. extern int trnsattr;
  23.  
  24. /* ******************************************************** fileout */
  25. /* This routine opens a disk file and writes the marked lines in the*/
  26. /* transcript to that file.                                         */
  27. fileout()
  28. {
  29. char *lpt, fileout[25];
  30. struct lines *pt;
  31. int i;
  32. FILE *fp2;
  33.  
  34.    poscurs(23,1);                  /* read in filename to output to */
  35.    printf("     filename >                    <");
  36.    poscurs(23,17);
  37.    for (i = 0;(fileout[i] = getchar()) != '\n';++i);
  38.    fileout[i] = 0;                /* filename read in, ready to use */
  39.    fp2 = fopen(fileout,"w");                           /* open file */
  40.    pt = top;                        /* start at top of llinked list */
  41.    do {
  42.       lpt = pt->lineloc;                     /* line of text stored */
  43.       if (pt->marked){                  /* only output marked lines */
  44.          fputs(lpt,fp2);                           /* output a line */
  45.          fputs("\n",fp2);                         /* and a linefeed */
  46.       }
  47.       pt = pt->dn;                             /* get the next line */
  48.    } while (pt != NULL);
  49.    fflush(fp2);                           /* flush the file to disk */
  50.    fclose(fp2);                                   /* close the file */
  51.    poscurs(23,7);
  52.    printf("  input >                     "); 
  53. }
  54.  
  55. /* ********************************************************* filein */
  56. /* A diskfile is opened and read into the transcript window while   */
  57. /* all calculations are done as the file is input. If any errors are*/
  58. /* found, the calculations are not done, zero is returned as a      */
  59. /* result and the remainder of the file is read in. It is assumed   */
  60. /* that the equations are correct before the file was originally    */
  61. /* written.                                                         */
  62. char filenam[25] = "help";        /* default filename to start with */
  63. filein()
  64. {
  65. char filein[25];
  66. char *fc;
  67. int i;
  68. FILE *fp2;
  69.  
  70.    poscurs(23,1);                      /*read in filename for input */
  71.    printf("     filename >                    <");
  72.    poscurs(23,17);
  73.    for (i = 0; (filein[i] = getchar()) != '\n';++i);
  74.    filein[i] = 0;                /* filename read in , ready to use */
  75.    if (filein[0] == 0)                  /* if no filename was input */
  76.       strcpy(filein,filenam);            /* use last valid filemane */
  77.    else
  78.       strcpy(filenam,filein);                 /* save for later use */
  79.    fp2 = fopen(filein,"r");                            /* open file */
  80.    if (fp2 == NULL) {                         /* file doesn't exist */
  81.       errcode = 11;
  82.       errout();
  83.    }
  84.    else {  
  85.       do {
  86.          fc = (fgets(inline,62,fp2));
  87.          if (fc == NULL) break;
  88.          for (i=0;inline[i];++i);
  89.          inline[i-1] = 0;
  90.           parse();
  91.          if ((ignore == 1) || errcode)
  92.             strtrans(inline,0);
  93.          else
  94.             strtrans(inline,1); 
  95.          transout();
  96.       } while (i != NULL);
  97.    }
  98.    for (i = 0;i < 200;++i) inline[i] = 0;       /* clear input area */
  99.    poscurs(23,7);
  100.    printf("  input >                      ");
  101. }
  102.  
  103. /* ******************************************************* strtrans */
  104. /* A line from the input area or from a file input is stored in the */
  105. /* transcript area. It is stored in the transcript array here, and  */
  106. /* output to the transcript window in the "transout" function.      */
  107. /* This function uses a linked list to store the lines of data.     */
  108. strtrans(line,type)
  109. char line[];
  110. int type;
  111. {
  112. int i;
  113. long int temp;
  114. char *pt;
  115. char buffer[25];     /* this is long enough to include an overwrite */
  116. double xx;                                    /* temporary variable */
  117. extern FILE *prtfile;                          /* print file output */
  118.  
  119.    p = (struct lines *)malloc(sizeof(struct lines));
  120.    pt = (char *)malloc(1 + strlen(line));
  121.    if ((p == NULL) || (pt == NULL)) {              /* out of memory */
  122.       errcode = 13;
  123.       errout();
  124.    }
  125.  
  126.    else {                  /* there is enough memory for this entry */
  127.       if (top == NULL){                               /* first entry */
  128.           top = bot = p;
  129.           p->dn = NULL;
  130.           p->up = NULL;
  131.       }
  132.       else {                                  /* additional entries */
  133.           bot->dn = p;
  134.           p->up = bot;
  135.           p->dn = NULL;
  136.           bot = p;
  137.       }
  138.  
  139.       p->lineloc = pt;
  140.       i = strlen(line);
  141.       p->isvalue = type;  
  142.       p->marked = type;
  143.       p->linelngt = i;
  144.       if (type) {
  145.          xx = allvars[varinuse].value;
  146.          if (xx < 0.0) xx = -xx;
  147.          if ((xx > 9999999.0) || (xx < .001))
  148.             sprintf(buffer,"%12.5e",allvars[varinuse].value);
  149.          else
  150.             sprintf(buffer,"%12.6f",allvars[varinuse].value);
  151.          buffer[12] = 0;
  152.          if (varinuse > 5) {                /* variable I through N */
  153.             temp = allvars[varinuse].value;
  154.             temp = temp & 077777777;
  155.             if (allvars[varinuse].outtype == 'D')
  156.                sprintf(buffer,"(D) %8ld",temp);
  157.             if (allvars[varinuse].outtype == 'O')
  158.                sprintf(buffer,"(O) %8lo",temp);
  159.             if ((allvars[varinuse].outtype == 'X') ||
  160.                 (allvars[varinuse].outtype == 'H'))
  161.                sprintf(buffer,"(H) %8lx",temp);
  162.          }
  163.          strcpy(p->strval,buffer);
  164.       }
  165.       else
  166.          strcpy(p->strval,"            ");
  167.       line[i] = '\0';                            /* line terminator */
  168.       strcpy(pt,line);
  169.       if (type && printit){
  170.          fprintf(prtfile,"%13s  %-62s\n",buffer,line);  
  171.       }
  172.       arrow = p;
  173.       trnsend = p;
  174.       lastline++;
  175.       arrowln = lastline;
  176.    }
  177. }
  178.  
  179. /* ******************************************************* transout */
  180. /* This function outputs the transcript to the transcript window    */
  181. extern char strngout[];
  182. transout()
  183. {
  184. int i;
  185. int maxm = 13;       /* number of lines to output to the trans wind */
  186. char *pt;
  187.    p = trnsend;
  188.    for (i = 0;i < maxm;++i){           /* count up max from trnsend */
  189.       if (p->up == NULL) break;                /* stop if top found */
  190.       p = p->up;
  191.    }
  192.    for (i = 0;i <= maxm;++i){       /* output max fields to viddisp */
  193.       pt = p->lineloc;                 /* pt now points to the line */
  194.       strcpy(strngout,p->strval);
  195.       strngdis(8+i,1,trnsattr);       /* output the formatted value */
  196.       strcpy(strngout,pt);
  197.       blnkline(8+i,16);                     /* write blanks to line */
  198.       if (p->marked)
  199.          chardis(8+i,15,trnsattr,'*');          /* marked indicator */
  200.       else 
  201.          chardis(8+i,15,trnsattr,' ');                     /* blank */
  202.       strngdis(8+i,17,trnsattr);
  203.       if (arrow == p)
  204.          chardis(8+i,14,trnsattr,16);                 /* arrow char */
  205.       else
  206.          chardis(8+i,14,trnsattr,' ');                     /* blank */
  207.       if (p->dn == NULL) break;             /* stop if bottom found */
  208.       p = p->dn;
  209.    }
  210.    poscurs(23,3);
  211.    printf("%4d",arrowln);
  212. }
  213.  
  214. /* ******************************************************* movarrow */
  215. /* This function is used to move the arrow up or down in the window */
  216. /* and to control where the window begins and ends in the transcript*/
  217. /* data. The arrow is always two lines from the top or bottom if it */
  218. /* is possible to do so.                                            */
  219. movarrow(where)
  220. int where;
  221. {
  222. int index;
  223. struct lines *temp;
  224. int iend, iarrow, itrnsend; 
  225.    iend = iarrow = itrnsend = 0;
  226.    if (where > 0) {
  227.       for (index = where;index && (arrow != bot);--index)
  228.          arrow = arrow->dn;                  /* move arrow down one */
  229.       for (temp = top,index = 0;temp != bot;index++) {
  230.          if (temp == arrow) iarrow = index;         /* locate arrow */
  231.          if (temp == trnsend) itrnsend = index;  /* loc display end */
  232.          temp = temp->dn;
  233.       }
  234.       if (temp == arrow) iarrow = index;          /* if they are at */
  235.       if (temp == trnsend) itrnsend = index;      /* the bottom end */
  236.       iend = index; 
  237.            /* now trnsend must be >= arrow, but not by more than 10 */
  238.       if (iarrow == iend) index = iend - itrnsend;
  239.       else if (itrnsend < (iarrow+1)) index = iarrow - itrnsend + 1;
  240.       else index = 0;       
  241.    }
  242.    else {
  243.       for (index = -where;index && (arrow != top);--index)
  244.          arrow = arrow->up;                    /* move arrow up one */
  245. /*    if (arrow == top) arrow = arrow->dn;      move one field down */
  246.       for (temp = top,index = 0;temp != bot;index++) {
  247.          if (temp == arrow) iarrow = index;         /* locate arrow */
  248.          if (temp == trnsend) itrnsend = index;  /* loc display end */
  249.          temp = temp->dn;
  250.       }
  251.       if (temp == arrow) iarrow = index;          /* if they are at */
  252.       if (temp == trnsend) itrnsend = index;      /* the bottom end */
  253.       iend = index; 
  254.            /* now trnsend must be >= arrow, but not by more than 12 */
  255.       if (iarrow == 0) index = (iend > 13?13:iend) - itrnsend;
  256.       else if ((itrnsend - iarrow) > 12) index = iarrow-itrnsend+12;
  257.       else index = 0;
  258.    }
  259.    if (index > 0)
  260.       for (;index > 0;--index)
  261.          trnsend = trnsend->dn;
  262.    else if (index < 0)
  263.       for (;index < 0;++index)
  264.          trnsend = trnsend->up;
  265.    arrowln = iarrow;
  266.    transout(); 
  267. }
  268.